package com.monolithiot.sso.service.impl;

import com.monolithiot.sso.common.entity.User;
import com.monolithiot.sso.common.exception.AlreadyExistedException;
import com.monolithiot.sso.common.exception.ServiceUnforeseenException;
import com.monolithiot.sso.common.util.EntityUtil;
import com.monolithiot.sso.common.util.MD5Util;
import com.monolithiot.sso.common.util.UploadFileUtil;
import com.monolithiot.sso.conf.prop.UploadFileConfigureProperties;
import com.monolithiot.sso.dao.BaseMapper;
import com.monolithiot.sso.dao.mapper.UserMapper;
import com.monolithiot.sso.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * 2018/12/10 Create by 郭文梁
 * UserServiceImpl
 * 用户业务实现
 *
 * @author 郭文梁
 * @date 2018/12/10
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements UserService {
    private final UserMapper userMapper;
    private final UploadFileConfigureProperties uploadFileConfigureProperties;

    @Autowired
    public UserServiceImpl(UserMapper userMapper, UploadFileConfigureProperties uploadFileConfigureProperties) {
        this.userMapper = userMapper;
        this.uploadFileConfigureProperties = uploadFileConfigureProperties;
    }

    @Override
    BaseMapper<User> getMapper() {
        return userMapper;
    }

    @Override
    public User register(User user) throws AlreadyExistedException {
        //检查参数
        EntityUtil.notNull(user, "name", "password");

        //检查用户是否已注册
        User query = new User();
        query.setName(user.getName());
        query.setMobileNumber(user.getMobileNumber());
        query.setPhoneNumber(user.getPhoneNumber());
        query.setEmail(user.getEmail());
        checkNotExists(query);

        //加密密码
        user.setPassword(MD5Util.md5(user.getPassword()));
        user.setLevelsOfImportance(User.DEFAULT_LEVELS_OF_IMPORTANCE);
        user.setGender(User.GENDER_UNKNOWN);
        //保存用户
        User res = save(user);
        if (res == null) {
            throw new ServiceUnforeseenException("Could not save user : " + user);
        }
        return res;
    }

    @Override
    public void checkNotExists(User query) throws AlreadyExistedException {
        String res = userMapper.checkExists(query);

        if (res != null) {
            throw new AlreadyExistedException(res);
        }
    }

    @Override
    public User login(String account, String password) {
        String passwordMd5 = MD5Util.md5(password);
        return userMapper.selectForLogin(account, passwordMd5);
    }

    @Override
    public User updateUserInfo(User user) throws AlreadyExistedException {
        String msg = userMapper.checkExistsExclude(user, user.getId());
        if (msg != null) {
            throw new AlreadyExistedException(msg);
        }
        User res = update(user);
        if (res == null) {
            throw new ServiceUnforeseenException("Could not update user:" + user);
        }
        return res;
    }

    @Override
    public User setHeadPortrait(User user, MultipartFile hpFile) throws IOException {
        assert !hpFile.isEmpty();
        File path = new File(uploadFileConfigureProperties.getBasePath(),
                uploadFileConfigureProperties.getUserHeadPortrait());
        String filename = UploadFileUtil.copyTmpFileTo(hpFile, path.getAbsolutePath());
        user.setHeadPortrait(filename);
        User res = update(user);
        if (res == null) {
            throw new ServiceUnforeseenException("Could update user:" + user);
        }
        return res;
    }
}
